package com.google.coffeeshop.gui.desktop.orderpayment.seatdetail;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.sourceforge.jdatepicker.JDatePicker;
import vn.bang.utility.DateUtil;
import vn.bang.utility.StringUtil;

import com.google.coffeeshop.SeatStatus;
import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.DatePicker;
import com.google.coffeeshop.gui.desktop.ObservingTextField;
import com.google.coffeeshop.gui.desktop.SwingFactory;

public class SeatDetailOverPanel extends JPanel {
	private JTextField tfCode;
	private JTextField tfName;
	private SeatDTO seatDTO = null;
	
	public SeatDetailOverPanel(SeatDTO seatDTO) {
		this.seatDTO = seatDTO;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {10, 30, 50};
		gridBagLayout.rowHeights = new int[] {0, 20, 20, 20, 50, 20};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		setLayout(gridBagLayout);
		
		JLabel lblTotalAmount = new JLabel("Total Amount");
		GridBagConstraints gbc_lblTotalAmount = new GridBagConstraints();
		gbc_lblTotalAmount.insets = new Insets(0, 0, 5, 5);
		gbc_lblTotalAmount.anchor = GridBagConstraints.EAST;
		gbc_lblTotalAmount.gridx = 1;
		gbc_lblTotalAmount.gridy = 0;
		add(lblTotalAmount, gbc_lblTotalAmount);
		
		tfTotalAmount = new JTextField();
		tfTotalAmount.setEditable(Boolean.FALSE);
		GridBagConstraints gbc_tfTotalAmount = new GridBagConstraints();
		gbc_tfTotalAmount.insets = new Insets(0, 0, 5, 0);
		gbc_tfTotalAmount.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfTotalAmount.gridx = 2;
		gbc_tfTotalAmount.gridy = 0;
		add(tfTotalAmount, gbc_tfTotalAmount);
		tfTotalAmount.setColumns(10);
		
		JLabel lblCode = new JLabel("Code");
		GridBagConstraints gbc_lblCode = new GridBagConstraints();
		gbc_lblCode.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblCode.insets = new Insets(0, 0, 5, 5);
		gbc_lblCode.gridx = 0;
		gbc_lblCode.gridy = 1;
		add(lblCode, gbc_lblCode);
		
		tfCode = new JTextField();
		tfCode.setEditable(Boolean.FALSE);
		GridBagConstraints gbc_tfCode = new GridBagConstraints();
		gbc_tfCode.gridwidth = 2;
		gbc_tfCode.fill = GridBagConstraints.BOTH;
		gbc_tfCode.anchor = GridBagConstraints.WEST;
		gbc_tfCode.insets = new Insets(0, 0, 5, 0);
		gbc_tfCode.gridx = 1;
		gbc_tfCode.gridy = 1;
		add(tfCode, gbc_tfCode);
		tfCode.setColumns(10);
		
		JLabel lblStatus = new JLabel("Status");
		GridBagConstraints gbc_lblStatus = new GridBagConstraints();
		gbc_lblStatus.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblStatus.insets = new Insets(0, 0, 5, 5);
		gbc_lblStatus.gridx = 0;
		gbc_lblStatus.gridy = 2;
		add(lblStatus, gbc_lblStatus);
		
		tfStatus = new JTextField();
		tfStatus.setEditable(Boolean.FALSE);
		GridBagConstraints gbc_tfStatus = new GridBagConstraints();
		gbc_tfStatus.gridwidth = 2;
		gbc_tfStatus.insets = new Insets(0, 0, 5, 0);
		gbc_tfStatus.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfStatus.gridx = 1;
		gbc_tfStatus.gridy = 2;
		add(tfStatus, gbc_tfStatus);
		tfStatus.setColumns(10);
		
		JLabel lblName = new JLabel("Name");
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 3;
		add(lblName, gbc_lblName);
		
		
		tfName = new JTextField();
		tfName.setEditable(Boolean.FALSE);
		GridBagConstraints gbc_tfName = new GridBagConstraints();
		gbc_tfName.gridwidth = 2;
		gbc_tfName.anchor = GridBagConstraints.NORTH;
		gbc_tfName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfName.insets = new Insets(0, 0, 5, 0);
		gbc_tfName.gridx = 1;
		gbc_tfName.gridy = 3;
		add(tfName, gbc_tfName);
		tfName.setColumns(10);
		
		JLabel lblDescription = new JLabel("Description");
		GridBagConstraints gbc_lblDescription = new GridBagConstraints();
		gbc_lblDescription.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblDescription.insets = new Insets(0, 0, 5, 5);
		gbc_lblDescription.gridx = 0;
		gbc_lblDescription.gridy = 4;
		add(lblDescription, gbc_lblDescription);
		
		taDescription = new JTextArea();
		taDescription.setEditable(Boolean.FALSE);
		GridBagConstraints gbc_taDescription = new GridBagConstraints();
		gbc_taDescription.gridwidth = 2;
		gbc_taDescription.insets = new Insets(0, 0, 5, 0);
		gbc_taDescription.fill = GridBagConstraints.BOTH;
		gbc_taDescription.gridx = 1;
		gbc_taDescription.gridy = 4;
		add(taDescription, gbc_taDescription);
		
		JLabel lblCreatedDate = new JLabel("Created Date");
		GridBagConstraints gbc_lblCreatedDate = new GridBagConstraints();
		gbc_lblCreatedDate.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblCreatedDate.insets = new Insets(0, 0, 0, 5);
		gbc_lblCreatedDate.gridx = 0;
		gbc_lblCreatedDate.gridy = 5;
		add(lblCreatedDate, gbc_lblCreatedDate);
		
		tfCreatedDate = new ObservingTextField();
		tfCreatedDate.setText("18/12/2013");
		tfCreatedDate.setEditable(false);
		tfCreatedDate.setColumns(10);
		GridBagConstraints gbc_tfCreatedDate = new GridBagConstraints();
		gbc_tfCreatedDate.gridwidth = 2;
		gbc_tfCreatedDate.anchor = GridBagConstraints.NORTH;
		gbc_tfCreatedDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfCreatedDate.gridx = 1;
		gbc_tfCreatedDate.gridy = 5;
		add(tfCreatedDate, gbc_tfCreatedDate);
		
		setData(seatDTO);
	}

	public void setTotalAmount(long amount) {
		tfTotalAmount.setText(String.valueOf(amount));
	}
	
	private void setData(SeatDTO seat) {
		tfCode.setText(seat.getCode());
		tfName.setText(seat.getSeatName());
		tfStatus.setText(SeatStatus.getSeatStatus(seat.getStatus()).name());
		taDescription.setText(seat.getDescription());
		tfCreatedDate.setText(DateUtil.toString(seat.getCreatedDate()));
	}
	
	public void saveSeat() {
		collectSeatData(seatDTO);
		OrderBusiness.saveSeat(seatDTO);
	}
	
	private Locale getLocale(String locate) {
		if(locate!=null && locate.length()>0)
			return new Locale(locate);
		else return Locale.FRANCE;
	}
	
	private void collectSeatData(SeatDTO seat) {
		seat.setCode(tfCode.getText());
		seat.setSeatName(tfName.getText());
		seat.setDescription(taDescription.getText());
		seat.setCreatedDate(DateUtil.toDate(tfCreatedDate.getText()));
		seat.setStatus(SeatStatus.valueOf(tfStatus.getText()).getStatusId());
	}
	
	private static final long serialVersionUID = 1L;
	private JTextField tfStatus;
	private JTextArea taDescription;
	private ObservingTextField tfCreatedDate;
	private JTextField tfTotalAmount;

}
